import os
import time
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from config import BASE_PATH
from base import log


class Base:
    # 初始化方法
    def __init__(self, driver):
        self.driver = driver

    # 查找元素方法
    def base_find(self, loc, timeout=5, poll_frequency=0.5):
        log.info("正在查找的元素为.....: {}".format(loc))
        # 用显示等待查找元素
        return WebDriverWait(self.driver, timeout, poll_frequency).until(lambda x: x.find_element(*loc))

    # 查找元素集合的方法
    def base_finds(self, loc, timeout=3, poll_frequency=0.5):
        log.info("正在查找的元素集合为.....: {}".format(loc))
        # 用显示等待查找元素
        return WebDriverWait(self.driver, timeout, poll_frequency).until(lambda x: x.find_elements(*loc))

    # 查找toast元素---todo 目前此方法存在问题获取不到toast
    def base_toast_text(self, toastmessage,timeout=10, poll_frequency=0.5):
        toast_loc = ("xpath", "//*[contains(@text,'%s')]" % toastmessage)
        try:
            ele = WebDriverWait(self.driver, timeout, poll_frequency).until(EC.presence_of_element_located(toast_loc))
            log.info("查找到toast----%s" % toast_loc)
            return ele.get_attribute("text")

        except Exception as e:
            log.info("未查找到toast----%s" % toast_loc)
            # raise e
            return False

    # 点击方法
    def base_click(self, loc):
        log.info("正在调用点击方法.....: 元素为:{}".format(loc))
        self.base_find(loc).click()

    # 输入方法
    def base_input(self, loc, value):
        log.info("正在调用输入方法.....: 元素为:{}  输入内容为:{}".format(loc, value))
        # 查找元素
        el = self.base_find(loc)
        # 清空
        el.clear()
        # 输入
        el.send_keys(value)

    # 获取文本
    def base_get_text(self, loc):
        log.info("正在调用获取文本方法.....: 元素为:{}".format(loc))
        return self.base_find(loc).text

    # 截图
    def base_get_img(self):
        img_patn = BASE_PATH + os.sep + "img" + os.sep + time.strftime("%Y_%m_%d_%H_%M_%S")
        log.info("正在调用截图方法.....: 错误图片截图为:{}.png".format(img_patn))
        self.driver.save_screenshot("{}.png".format(img_patn))
        return img_patn + ".png"  # 返回图片地址

    # 切换frame
    def base_switch_to_frame(self, loc):
        log.info("正在调用切换frame方法.....: 元素为:{}".format(loc))
        # 查找frome元素
        el = self.base_find(loc)
        # 切换frame页
        self.driver.switch_to_frame(el)

    # 恢复默认窗口
    def base_switch_to_default_window(self):
        log.info("正在调用恢复默认窗口方法.....")
        self.driver.switch_to_default_content()

    # 获取屏幕尺寸
    def base_get_window_size(self):
        log.info("正在调用获取屏幕尺寸方法.....")
        return self.driver.get_window_size()

    # 通过手势上滑到指定位置-坐标
    def base_swipe(self, x1, y1, x2, y2, duration=1000):
        log.info(f"正在调用手势上滑方法......:目标位置为:({x1,y1})，起始位置为:({x2,y2})")
        self.driver.swipe(x2, y2, x1, y1, duration)

    # 通过指定位置点击-坐标
    def base_tap(self, x, y):
        log.info(f"正在调用点击指定位置访问......:目标位置为({x,y}):")
        self.driver.tap([(x, y)])
